<template>
  <div class="app-container">
    <div class="formDiv">
      <el-form ref="form" :inline="true" label-width="140px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="手机号">
              <el-input v-model="params.username"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="真实姓名">
              <el-input v-model="params.realname"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-button style="margin-left:60px" type="primary" @click="searchFn">查询</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="listDiv">
      <el-table :data="tableData" class="tableData" style="width: 100%">
        <el-table-column prop="username" label="手机号" align="center">
          <template slot-scope="scope"><span>{{scope.row.username |phonereg}}</span></template>
        </el-table-column>
        <el-table-column prop="realname" label="用户名" align="center" />
        <el-table-column prop="type" label="类型" align="center">
          <template slot-scope="scope"><span v-if="scope.row.type">方案{{scope.row.type |toChinesNum}}</span></template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button v-if="scope.row.type" @click="checkfn(scope.row)" type="text">重置</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <el-pagination
        :current-page="params.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { unprocessedApplyList, delProcessedApply } from "@/api/money";
export default {
  name: "",
  data() {
    return {
      permissionJson: JSON.parse(localStorage.getItem("permissionJson")),
      params: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      tableData: []
    };
  },
  created() {
    this.list();
  },
  filters: {
    toChinesNum(num) {
      if (num) {
        let changeNum = [
          "零",
          "一",
          "二",
          "三",
          "四",
          "五",
          "六",
          "七",
          "八",
          "九"
        ];
        let unit = ["", "十", "百", "千", "万"];
        num = parseInt(num);
        let getWan = temp => {
          let strArr = temp
            .toString()
            .split("")
            .reverse();
          let newNum = "";
          for (var i = 0; i < strArr.length; i++) {
            newNum =
              (i == 0 && strArr[i] == 0
                ? ""
                : i > 0 && strArr[i] == 0 && strArr[i - 1] == 0
                ? ""
                : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i])) +
              newNum;
          }
          return newNum;
        };
        let overWan = Math.floor(num / 10000);
        let noWan = num % 10000;
        if (noWan.toString().length < 4) {
          noWan = "0" + noWan;
        }
        return overWan ? getWan(overWan) + "万" + getWan(noWan) : getWan(num);
      }
    }
  },
  methods: {
    //删除
    checkfn(datas) {
      console.log(datas);
      this.$alert("确定重置该条数据吗？", "提示", {
        confirmButtonText: "确定",
        callback: async action => {
          if (action == "confirm") {
            let res = await delProcessedApply({ id: datas.userId });
            this.$message.warning(res.data.msg);
            if (res.data.code == 0) this.list();
          }
        }
      });
    },
    searchFn() {
      this.params.pageNum = 1;
      this.list();
    },
    async list() {
      let res = await unprocessedApplyList(this.params);
      if (res.data.code == 0) {
        this.tableData = res.data.data.list;
        this.total = res.data.data.count;
      }
    },
    handleSizeChange(val) {
      this.params.pageNum = 1;
      this.params.pageSize = val;
      this.list();
    },
    handleCurrentChange(val) {
      this.params.pageNum = val;
      this.list();
    }
  }
};
</script>
